package com.xuxin.nantai.activity.chou;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.xuxin.nantai.R;
import com.xuxin.nantai.base.BaseActivity;
import com.xuxin.nantai.base.BaseConstant;
import com.xuxin.nantai.bean.BaseBean;
import com.xuxin.nantai.bean.SelectDetailBean;
import com.xuxin.nantai.contract.SelectDetailContract;
import com.xuxin.nantai.presenter.SelectDetailPresenter;
import com.xuxin.nantai.utils.ACache;

import org.jetbrains.annotations.NotNull;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xu_xin
 * Created on 2020.06.12 0012
 * 缝制抽检明细
 */
public class SelectDetailActivity extends BaseActivity implements SelectDetailContract.View {
    private SelectDetailContract.Presenter presenter = new SelectDetailPresenter(this);
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;
    @BindView(R.id.title_name)
    TextView title_name;

    @BindView(R.id.smart_refresh)
    SmartRefreshLayout smart_refresh;
    @BindView(R.id.smart_recycle)
    RecyclerView smart_recycle;
    @BindView(R.id.smart_empty)
    ImageView smart_empty;

    private SelectDetailAdapter myAdapter;
    private String ID;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_smart);
    }

    @Override
    public void initView() {
        title_name.setText("缝制抽检明细");
        title_right.setVisibility(View.VISIBLE);
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            ID = bundle.getString("ID");
            LogUtils.e("缝制抽检明细_ID = " + ID);
        }
        title_right.setVisibility(View.VISIBLE);
        smart_refresh.setEnableLoadMore(false);
        smart_refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                initData();
            }
        });
        myAdapter = new SelectDetailAdapter();
        smart_recycle.setLayoutManager(new LinearLayoutManager(this));
        smart_recycle.setItemViewCacheSize(BaseConstant.page_limit);
        smart_recycle.setNestedScrollingEnabled(false);
        smart_recycle.setAdapter(myAdapter);
        myAdapter.addChildClickViewIds(R.id.item_select_detail_update, R.id.item_select_detail_delete);
        myAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            switch (view.getId()) {
                case R.id.item_select_detail_update:
                    try {
                        Bundle bundle_update = new Bundle();
                        bundle_update.putString("HID", ID);
                        bundle_update.putString("Guid", myAdapter.getData().get(position).getGuid());
                        bundle_update.putString("DID", myAdapter.getData().get(position).getDID());
                        bundle_update.putString("ProductName", myAdapter.getData().get(position).getProductorName());
                        bundle_update.putString("ProductCode", myAdapter.getData().get(position).getProductCode());
                        bundle_update.putString("DefectNum", myAdapter.getData().get(position).getDefectNum());
                        bundle_update.putString("DefectDes", myAdapter.getData().get(position).getDefectDes());
                        ActivityUtils.startActivityForResult(bundle_update, SelectDetailActivity.this,
                                SelectDetailUpdateActivity.class, BaseConstant.result_code);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case R.id.item_select_detail_delete:
                    new MaterialDialog.Builder(SelectDetailActivity.this)
                            .content("确定删除这条数据？")
                            .positiveText("确定")
                            .negativeText("取消")
                            .onPositive((dialog, which) -> {
                                try {
                                    Message message = Message.obtain();
                                    message.what = 1;
                                    Bundle data = new Bundle();
                                    data.putString("Guid", myAdapter.getData().get(position).getGuid());
                                    message.setData(data);
                                    handler.sendMessage(message);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            })
                            .show();
                    break;
            }
        });
    }

    @Override
    public void initData() {
        myAdapter.setList(null);
        handler.sendEmptyMessage(0);
    }

    @Override
    public void onComplete() {
        smart_refresh.finishRefresh();
        smart_empty.setVisibility(myAdapter.getData().isEmpty() ? View.VISIBLE : View.GONE);
    }

    @Override
    @OnClick({R.id.title_left, R.id.title_right})
    public void onClick(View view) {
        KeyboardUtils.hideSoftInput(this);
        switch (view.getId()) {
            case R.id.title_left:
                finish();
                break;
            case R.id.title_right:
                try {
                    Bundle bundle_update = new Bundle();
                    bundle_update.putString("HID", ID);
                    ActivityUtils.startActivityForResult(bundle_update, SelectDetailActivity.this,
                            SelectDetailUpdateActivity.class, BaseConstant.result_code);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    private Handler handler = new Handler(message -> {
        String UserName = ACache.get(this).getAsString("UserName");
        switch (message.what) {
            case 0:
                presenter.getSelectDetail(ID);
                break;
            case 1:
                String Guid = message.getData().getString("Guid");
                LogUtils.e("Guid = " + Guid);
                presenter.delete(UserName, Guid);
                break;
        }
        return false;
    });

    @Override
    public void onSuccess(SelectDetailBean selectDetailBean) {
        myAdapter.setList(selectDetailBean.getData());
    }

    @Override
    public void onSuccess(BaseBean baseBean) {
        ToastUtils.showShort(baseBean.getMessage());
        setResult(BaseConstant.result_code);
        initData();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (BaseConstant.result_code == resultCode) {
            LogUtils.e("返回刷新");
            initData();
            setResult(BaseConstant.result_code);
        }
    }

    private static class SelectDetailAdapter extends BaseQuickAdapter<SelectDetailBean.DataBean, BaseViewHolder> {

        private SelectDetailAdapter() {
            super(R.layout.item_select_detail);
        }

        @Override
        protected void convert(@NotNull BaseViewHolder holder, SelectDetailBean.DataBean item) {
            holder.setText(R.id.item_select_detail_code, "检验员编号 : " + item.getProductCode());
            holder.setText(R.id.item_select_detail_name, "检验员 : " + item.getProductorName());
            holder.setText(R.id.item_select_detail_num, "疵点数量 : " + item.getDefectNum());
            holder.setText(R.id.item_select_detail_defect, "瑕疵描述 : " + item.getDefectDes());
            holder.setText(R.id.item_select_detail_code, "检验员编号 : " + item.getProductCode());
        }
    }
}